//
//  MaintenceCalendarView.swift
//  koolcar
//
//  Created by xiantian on 2023/10/24.
//

import UIKit
//typealias MaintenceCalendarViewBlock = ()->Void
class MaintenceCalendarView: UIView {
    var selectIndex: NSInteger = 9999
    var weekView: UIView! //周几
    var collectionView: UICollectionView! //日期
    var scrollView: UIScrollView!
    var data: CalendarItemData!
    var bkgView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = UIColor.white
        return tempView
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = Theme.viewBkgGrayColor
        self.addSubview(bkgView)
        
        self.weekView = self.createWeekHeader()
        self.bkgView.addSubview(weekView)
   
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize.init(width: (kScreenWidth - 28 - 6)/7, height: 54)
        layout.minimumLineSpacing = 1
        layout.minimumInteritemSpacing = 1
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 10, width: kScreenWidth, height: 54), collectionViewLayout: layout)
        self.collectionView.register(MaintenceCalendarCell.self, forCellWithReuseIdentifier: "MaintenceCalendarCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.white
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        bkgView.addSubview(self.collectionView)
        
        self.addSnpLayout()
        self.collectionView.reloadData()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        weekView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(30)
            make.top.equalTo(10)
        }
        
        self.collectionView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.top.equalTo(weekView.snp.bottom)
            make.height.greaterThanOrEqualTo(53)
            make.bottom.equalTo(0).priority(.high)
        }
        
        self.layoutIfNeeded()
    }
    
    private func createWeekHeader() -> UIView {
        let tempView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 30))
        tempView.backgroundColor = .white
        self.addSubview(tempView)
        
        let itemWidth: CGFloat = (kScreenWidth - 28)/7
        for index in 1 ... 7 {
            var tempWeek = ""
            switch index {
                case 1:
                    tempWeek = MMLocalizedString("Sun", comment: "周日")
                case 2:
                    tempWeek = MMLocalizedString("Mon", comment: "周一")
                case 3:
                    tempWeek = MMLocalizedString("Tue", comment: "周二")
                case 4:
                    tempWeek = MMLocalizedString("Wed", comment: "周三")
                case 5:
                    tempWeek = MMLocalizedString("Thu", comment: "周四")
                case 6:
                    tempWeek = MMLocalizedString("Fri", comment: "周五")
                case 7:
                    tempWeek = MMLocalizedString("Sat", comment: "周六")
                default:
                    ()
            }
            
            let tempLab = UILabel()
            tempLab.textColor = UIColor.init(hex: 0x939AA8)
            tempLab.backgroundColor = UIColor.clear
            tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
            tempLab.numberOfLines = 1
            tempLab.lineBreakMode = .byTruncatingTail
            tempLab.textAlignment = .center
            tempLab.text = tempWeek
            
            tempLab.frame = CGRect.init(x: itemWidth * CGFloat(index-1) + 14, y: 0, width: itemWidth, height: 30)
            tempView.addSubview(tempLab)
        }
        return tempView
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension MaintenceCalendarView: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data?.dates.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MaintenceCalendarCell", for: indexPath) as! MaintenceCalendarCell
        let tempData = self.data.dates
        if tempData.count > indexPath.row {
            let itemModel = tempData[indexPath.row]
            cell.titleLab.text = itemModel.day
            if itemModel.enable ?? false {
                cell.titleLab.textColor = Theme.titleLabColor
            }else{
                cell.titleLab.textColor = Theme.lightNineColor
            }
            
            if itemModel.select {
                cell.bkgView.backgroundColor = UIColor.init(hex: 0x71D883, alpha: 0.3)
            }else{
                cell.bkgView.backgroundColor = .white
            }
        }
      
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let dataArr = self.data.dates
        guard dataArr.count > indexPath.row else {
            return
        }
        let item = dataArr[indexPath.row]
        guard item.enable ?? false else{
            return
        }
//        self.selectIndex = indexPath.row
        item.selectBlock?()
        self.collectionView.reloadData()
    }
}
